<template>
  <div class="mod-course">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item v-if="roles==0">
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="dataListLoading" :data="tableData" border style="width: 100%;">
      <el-table-column prop="_id" header-align="center" align="center" width="240" label="ID"/>
      <el-table-column prop="courseName" header-align="center" align="center" label="课程名"/>
      <el-table-column prop="status" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <span v-if="scope.row.status=='0'">未发布</span>
          <span v-if="scope.row.status=='1'">已发布</span>
          <span v-if="scope.row.status=='2'">已开课</span>
        </template>
      </el-table-column>
      <el-table-column prop="teacher.username" header-align="center" align="center" label="老师"/>
      <el-table-column prop="credit" header-align="center" align="center" label="学分"/>
      <el-table-column prop="remark" header-align="center" align="center" label="备注"/>
      <el-table-column header-align="center" align="center" width="160" label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status==0&&roles==0"
            type="text"
            size="small"
            @click="changeStatus(1,scope.row)"
          >发布</el-button>
          <el-button
            v-if="scope.row.status==1&&roles==0"
            type="text"
            size="small"
            @click="changeStatus(0,scope.row)"
          >取消发布</el-button>
          <el-button
            v-if="roles==2&&$route.name == 'UnselCourse'"
            type="text"
            size="small"
            @click="selCourse(scope.row._id,'add')"
          >选课</el-button>
          <el-button
            v-if="roles==2&&$route.name == 'SelCourse'&&scope.row.status==1"
            type="text"
            size="small"
            @click="selCourse(scope.row._id,'remove')"
          >取消选课</el-button>
          <el-button
            v-if="scope.row.status==1&&roles==1"
            type="text"
            size="small"
            @click="changeStatus(2,scope.row)"
          >开课</el-button>
          <el-button
            v-if="scope.row.status==2&&roles==1"
            type="text"
            size="small"
            @click="assessment(scope.row)"
          >考核</el-button>
          <el-button
            :disabled="scope.row.role==0"
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row,true)"
          >查看</el-button>
          <el-button
            v-if="roles==0"
            :disabled="scope.row.role==0"
            type="text"
            size="small"
            @click="deleteHandle(scope.row._id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="pageNum"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
    />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"/>
    <!-- 成绩修改 -->
    <assessment-update v-if="assessmentVisible" ref="assessmentUpdate"/>
    <!-- 成绩查看 -->
    <assessment-view v-if="assViewVisible" ref="assessmentView"/>
  </div>
</template>

<script>
import AddOrUpdate from "./course-add-or-update";
import assessmentUpdate from "./assessment-update";
import assessmentView from "./assessment-view";
import { getCourses, delCourse, updateCourse } from "@/api/manage";
import { mapGetters } from "vuex";
export default {
  components: {
    AddOrUpdate,
    assessmentUpdate,
    assessmentView
  },
  data() {
    return {
      dataForm: {
        userName: ""
      },
      dataList: [],
      pageNum: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      assessmentVisible: false,
      assViewVisible: false
    };
  },
  computed: {
    ...mapGetters(["roles", "userId"]),
    tableData() {
      if (this.roles === "2") {
        if (this.$route.name === "UnselCourse") {
          return this.dataList.filter(element => {
            return !element.students.includes(this.userId);
          });
        } else {
          return this.dataList.filter(element => {
            return element.students.includes(this.userId);
          });
        }
      } else if (this.roles === "1") {
        return this.dataList.filter(element => {
          return element.status !== 0;
        });
      }
      return this.dataList;
    }
  },
  mounted() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let data = {
        pageNum: this.pageIndex,
        pageSize: this.pageSize
      };
      getCourses(data).then(res => {
        if (res.code === 1) {
          const data = res.data;
          this.dataList = data.courseLists;
          this.totalPage = data.totalPage;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageNum = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageNum = val;
      this.getDataList();
    },
    // 新增 / 修改
    addOrUpdateHandle(row, flag) {
      if (row && row.status == 2) {
        this.assViewVisible = true;
        this.$nextTick(() => {
          this.$refs.assessmentView.init(row);
        });
      } else {
        this.addOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(row, flag);
        });
      }
    },
    // 删除
    deleteHandle(id) {
      var courseId = id;
      // ? [id]
      // : this.dataListSelections.map(item => {
      //     return item.userId;
      //   });
      this.$confirm(`确定对id=${id}进行删除操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          delCourse({ courseId }).then(res => {
            if (res.code === 1) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                }
              });
            } else {
              const data = res.data;
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
    changeStatus(status, row) {
      const { students, _id } = row;
      if (status === 2 && students.length === 0) {
        this.$message({
          message: "没有学生选课不能开课",
          type: "error",
          duration: 1500
        });
        return;
      }
      updateCourse({ status, courseId: _id }).then(res => {
        if (res.code === 1) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.getDataList();
            }
          });
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    selCourse(courseId, flag) {
      const data = {
        courseId,
        userId: this.userId,
        flag
      };
      updateCourse(data).then(res => {
        if (res.code === 1) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.getDataList();
            }
          });
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    // 成绩考核
    assessment(row) {
      this.assessmentVisible = true;
      this.$nextTick(() => {
        this.$refs.assessmentUpdate.init(row);
      });
    }
  }
};
</script>
<style lang="scss">
.mod-course {
  padding: 20px;
}
</style>

